package org.batatasframework.support.definition.impl;

import org.batatasframework.support.database.FieldFormatSetter;
import org.batatasframework.support.database.metadatas.TableMetadata;
import org.batatasframework.support.definition.FieldDefinition;
import org.batatasframework.support.definition.TableDefinition;
import org.batatasframework.support.searchable.FieldDisplayType;
import org.batatasframework.support.searchable.FieldDisplayable;
import org.batatasframework.support.type.JavaType;

import cn.bonoon.kernel.util.StringHelper;

public abstract class AbstractFieldDefinition implements FieldDefinition, FieldDisplayable, FieldFormatSetter{

	/** 定义的用于唯一标识这个字段的关键词。字段？指标名？ */
	protected final String key;
	protected String[] group;
	/** 可用于显示的类型 */
	protected FieldDisplayType displayType = FieldDisplayType.ITEM;
	/** 这个字段所属的主表 */
	protected final TableDefinition tableDefinition;

	protected int ordinal;
	/** 名称，可用于界面上的显示，也可以不用 */
	protected String title;
	/** 自定义的格式化 */
	protected Object format;
	
	protected boolean must;
	
	protected Boolean unGroup;
	
	@Override
	public Boolean unGroup() {
		
		return unGroup;
	}
	
	@Override
	public boolean isMustDiaplay() {
		return must;
	}
	
	public AbstractFieldDefinition setMustDiaplay(boolean must){
		this.must = must;
		return this;
	}
	
	protected AbstractFieldDefinition(String key, TableDefinition tableDefinition) {
		this.key = key;
		this.tableDefinition = tableDefinition;
	}
	
	@Override
	public void setJavaType(JavaType javaType) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String getName() {
		return key;
	}

	@Override
	public boolean enabled() {
		return true;
	}

	@Override
	public TableMetadata getTableMetadata() {
		return tableDefinition;
	}
	
	@Override
	public Object getFormat() {
		return format;
	}
	
	@Override
	public String getTitle() {
		return StringHelper.get(title, key);
	}

	@Override
	public int getOrdinal() {
		return ordinal;
	}
	
	@Override
	public String[] getGroup() {
		return group;
	}

	@Override
	public String getKey() {
		return key;
	}
	@Override
	public String getcolumnName() {
		return key;
	}
	@Override
	public FieldDisplayType getDisplayType() {
		return displayType;
	}

	public AbstractFieldDefinition setDisplayType(FieldDisplayType displayType) {
		this.displayType = displayType;
		return this;
	}

	public AbstractFieldDefinition setGroup(String[] group) {
		this.group = group;
		return this;
	}

	@Override
	public boolean hasFormate() {
		return null != format;
	}

	@Override
	public void setFormat(Object format){
		if(null != format) this.format = format;
	}
	
	public AbstractFieldDefinition setOrdinal(int ordinal) {
		this.ordinal = ordinal;
		return this;
	}
	
	public AbstractFieldDefinition setTitle(String title) {
		if(StringHelper.isNotEmpty(title)) this.title = title;
		return this;
	}
	public AbstractFieldDefinition setUnGroup(Boolean unGroup) {
		this.unGroup = unGroup;
		return this;
	}
	
}
